package gw.com.android.ui.quote2;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import butterknife.ButterKnife;
import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.model.DataManager;
import gw.com.android.terminal.AppTerminal;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.app.FragmentsManagerUtil;
import www.com.library.app.PushMsgTabFragment;

/**
 * 内嵌在3.2新版 行情 分类viewPager里面的Fragment
 */
public class QuoteInsideFragment extends PushMsgTabFragment {

    public static String CODE_ID_TAG = "codeId";//产品id
    public static String NAME_TAG = "nameTag";//产品名字

    private QuoteSelfFragment mSelfFragment;
    private QuoteSelfFragment2 mSelfFragment2;

    private String mCurrentTag; // 当前显示的fragment
    private PushMsgTabFragment mCurrentFragment = null; // 当前显示的fragment

    public static boolean mIsRiseType = false;//切换
    private String mCodeId;//产品识别码
    private String mTypeName;//产品类型名字

    public static QuoteInsideFragment newInstance() {
        QuoteInsideFragment fragment = new QuoteInsideFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mCodeId = bundle.getString(CODE_ID_TAG);
        mTypeName = bundle.getString(NAME_TAG);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_viewpager_inside_quote;
    }

    @Override
    protected void initLayoutView() {
        ButterKnife.bind(this, mRootView);
        mIsRiseType = GTConfig.instance().getBooleanValue(GTConfig.PREF_RISE_QUOTE, false);
    }

    @Override
    protected void initViewData() {
        Log.d("refreshTableTag", "1");
        refreshTable();
    }

    public void refreshTable() {
        if (mIsRiseType) {
            showSelf2Fragment();
        } else {
            showSelfFragment();
        }
    }

    /**
     * 展示自选行情界面
     */
    private void showSelfFragment() {

        mCurrentTag = AppContances.TAB_SELF_QUOTE_TAG + "_" + mCodeId;
        mSelfFragment = (QuoteSelfFragment) FragmentsManagerUtil.instance().getFragmentbyTag(getActivity(), mCurrentTag);

        if (null == mSelfFragment) {
            Log.d("showSelfFragment", "create-1:" + mCurrentTag);
            mSelfFragment = QuoteSelfFragment.newInstance();
            Bundle bundle = new Bundle();
            bundle.putString(CODE_ID_TAG, mCodeId);
            bundle.putString(NAME_TAG, mTypeName);
            mSelfFragment.setArguments(bundle);
            FragmentsManagerUtil.instance().addPushMsgTabFragment(getActivity(), mSelfFragment, mCurrentTag);
        } else {
            Log.d("showSelfFragment", "ruse-1:" + mCurrentTag);
        }
        showFragment(mSelfFragment);
    }

    /**
     * 展示涨跌自选行情界面
     */
    private void showSelf2Fragment() {

        mCurrentTag = AppContances.TAB_SELF2_QUOTE_TAG + "_" + mCodeId;
        mSelfFragment2 = (QuoteSelfFragment2) FragmentsManagerUtil.instance().getFragmentbyTag(getActivity(), mCurrentTag);

        if (null == mSelfFragment2) {
            Log.d("showSelfFragment", "create-2:" + mCurrentTag);
            mSelfFragment2 = QuoteSelfFragment2.newInstance();
            Bundle bundle = new Bundle();
            bundle.putString(CODE_ID_TAG, mCodeId);
            mSelfFragment2.setArguments(bundle);
            FragmentsManagerUtil.instance().addPushMsgTabFragment(getActivity(), mSelfFragment2, mCurrentTag);
        } else {
            Log.d("showSelfFragment", "ruse-2:" + mCurrentTag);
        }
        showFragment(mSelfFragment2);
    }

    /**
     * 显示指定的fragment
     */
    private void showFragment(PushMsgTabFragment mBaseFragment) {
        if (mFragmentManager == null) {
            mFragmentManager = this.getChildFragmentManager();
        }
        mFragmentTransaction = mFragmentManager.beginTransaction();

        if (null == mFragmentManager.findFragmentByTag(mCurrentTag)) {
            mFragmentTransaction.add(R.id.contentFragmentInside, mBaseFragment, mCurrentTag);
        }
        if (null != mCurrentFragment) {
            mFragmentTransaction.hide(mCurrentFragment);
        }
        mFragmentTransaction.show(mBaseFragment);
        mFragmentTransaction.commitAllowingStateLoss();

        mCurrentFragment = mBaseFragment;
    }

    @Override
    public void registerRxBus() {
        Disposable switchRegister = RxBus.getInstance().registertoObservableSticky(GTSConst.REPLY_SWITCH_ACCOUNT + "", Boolean.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {

                    @Override
                    public void accept(@NonNull Boolean hasRefresh) throws Exception {
                        if (null != mCurrentFragment) {
                            mCurrentFragment.onSymbolNotify();
                        }
                    }
                });
        bindSubscription(switchRegister);

        //消息，要更换表头啦！
        Disposable switchHeaderRegister = RxBus.getInstance().registertoObservableSticky(GTSConst.REPLY_SWITCH_TABLE_HEADER + "", Boolean.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {

                    @Override
                    public void accept(@NonNull Boolean hasRefresh) throws Exception {
                        Log.d("refreshTableTag", "2");
                        refreshTable();

                    }
                });
        bindSubscription(switchHeaderRegister);
    }

    @Override
    public void onSendQuote() {
        AppTerminal.instance().sendQuoteSubscribe(DataManager.instance().getQuoteSubscribeCodes(mCodeId + ""));
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isVisible()) {
            Log.d("refreshTableTag", "3");
            refreshTable();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.d("onHiddenChanged", "" + hidden);
        if (!hidden) {
            onSendQuote();
            Log.d("refreshTableTag", "4");
            refreshTable();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            if (getActivity() != null)
                refreshTable();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
